<?php
class ControllerMenuMenu extends Controller {
	private $error = array();
	
	public function index() {
		$this->load->language('menu/menu');

		$this->document->title = $this->language->get('heading_title');

		$this->load->model('menu/menu');

		$this->getList();
	}
	
	public function insert() {
		$this->load->language('menu/menu');

		$this->document->title = $this->language->get('heading_title');

		$this->load->model('menu/menu');

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_menu_menu->addMenu($this->request->post);
			
			$this->session->data['success'] = $this->language->get('text_success');
			
			$url = '';

			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
			
			$this->redirect(HTTPS_SERVER . 'index.php?route=menu/menu&token=' . $this->session->data['token'] . $url);
    	}
    	
		$this->getForm();
	}
	
	public function update() {
		$this->load->language('menu/menu');

		$this->document->title = $this->language->get('heading_title');

		$this->load->model('menu/menu');
		
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_menu_menu->editMenu($this->request->get['menu_id'], $this->request->post);
			
			$this->session->data['success'] = $this->language->get('text_success');
			
			$url = '';
					
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
			
			$this->redirect(HTTPS_SERVER . 'index.php?route=menu/menu&token=' . $this->session->data['token'] . $url);
    	}
    	
		$this->getForm();
	}
	
	public function delete() {
		$this->load->language('menu/menu');

		$this->document->title = $this->language->get('heading_title');

		$this->load->model('menu/menu');
		
		if (isset($this->request->post['selected']) && $this->validateDelete()) {
      		foreach ($this->request->post['selected'] as $menu_id) {
				$this->model_menu_menu->deleteMenu($menu_id);	
			}

			$this->session->data['success'] = $this->language->get('text_success');
			
			$url = '';

			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
			
			$this->redirect(HTTPS_SERVER . 'index.php?route=menu/menu&token=' . $this->session->data['token'] . $url);
    	}
    	
		$this->getList();
	}
	
	private function getList() {
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'menuname';
		}

		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}
		
		$url = '';

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
			
		$this->document->breadcrumbs = array();

		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=common/home&token=' . $this->session->data['token'],
       		'text'      => $this->language->get('text_home'),
      		'separator' => FALSE
		);

		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=menu/menu&token=' . $this->session->data['token'] . $url,
       		'text'      => $this->language->get('heading_title'),
      		'separator' => ' :: '
      	);
      		 
      	$this->data['insert'] = HTTPS_SERVER . 'index.php?route=menu/menu/insert&token=' . $this->session->data['token'] . $url;
      	$this->data['delete'] = HTTPS_SERVER . 'index.php?route=menu/menu/delete&token=' . $this->session->data['token'] . $url;
      	$this->data['level_link'] = HTTPS_SERVER . 'index.php?route=menu/menu&token=' . $this->session->data['token'] . $url;
      	
		if (isset($this->request->post['level'])) {
      		$this->data['level'] = $this->request->post['level'];
    	} else {
      		$this->data['level'] = '10';
    	}
    	
      	$this->data['menus'] = array();

      	$data = array(
			'sort'  => $sort,
			'order' => $order
      	);
      	
      	$results = $this->model_menu_menu->getTopMenus($data);

      	foreach ($results as $result) {
      		$subMenuIds = $this->model_menu_menu->strToArray($this->model_menu_menu->getIds($result['menu_id'], $result['menu_id'].'/'), '/');
      		foreach ($subMenuIds as $subMenuId) {
      			if ($subMenuId != '') {
      				
      				$action = array();
      				$action_view_content = array();
      				
	      			$oneMenu = $this->model_menu_menu->getMenuById($subMenuId);
	      			
	      			if ($this->data['level'] - 1 >= $oneMenu['sublevel']) {
		      			$action[] = array(
							'text' => $this->language->get('text_edit'),
							'href' => HTTPS_SERVER . 'index.php?route=menu/menu/update&token=' . $this->session->data['token'] . '&menu_id=' . $oneMenu['menu_id'] . $url
		      			);
		      			$action_view_content[] = array(
							'text' => $this->language->get('text_view_content'),
							'href' => HTTPS_SERVER . 'index.php?route=content/content/&token=' . $this->session->data['token'] . '&menu_id=' . $oneMenu['menu_id']
		      			);
						
		      			$this->data['menus'][] = array(
							'menu_id'    => $oneMenu['menu_id'],
							'pid'   => $oneMenu['pid'],
		      				'menuname'   => $oneMenu['menuname'],
		      				'ordering'   => $oneMenu['ordering'],
		      				'sublevel'   => $oneMenu['sublevel'],
		      			    'content_count' => $this->model_menu_menu->getContentCountById($oneMenu['menu_id']),
		      				'createdate'   => date($this->language->get('date_format_short'), strtotime($oneMenu['createdate'])),
		      				'updatedate'   => date($this->language->get('date_format_short'), strtotime($oneMenu['updatedate'])),
							'selected'   => isset($this->request->post['selected']) && in_array($oneMenu['menu_id'], $this->request->post['selected']),
							'action'     => $action,
		      				'action_view_content'     => $action_view_content
		      			);
	      			}
      			}
      		}
      	}
      	
      	$this->data['heading_title'] = $this->language->get('heading_title');

      	$this->data['text_no_results'] = $this->language->get('text_no_results');

      	$this->data['column_no'] = $this->language->get('column_no');
      	$this->data['column_menuname'] = $this->language->get('column_menuname');
      	$this->data['column_ordering'] = $this->language->get('column_ordering');
      	$this->data['column_menu_id'] = $this->language->get('column_menu_id');
      	$this->data['column_content_count'] = $this->language->get('column_content_count');
      	$this->data['column_action'] = $this->language->get('column_action');

      	$this->data['select_root'] = $this->language->get('select_root');
      	
      	$this->data['button_insert'] = $this->language->get('button_insert');
      	$this->data['button_delete'] = $this->language->get('button_delete');

      	

      	if (isset($this->error['warning'])) {
      		$this->data['error_warning'] = $this->error['warning'];
      	} else {
      		$this->data['error_warning'] = '';
      	}
      	
      	if (isset($this->session->data['success'])) {
      		$this->data['success'] = $this->session->data['success'];

      		unset($this->session->data['success']);
      	} else {
      		$this->data['success'] = '';
      	}
      	
      	$url = '';

      	if ($order == 'ASC') {
      		$url .= '&order=DESC';
      	} else {
      		$url .= '&order=ASC';
      	}
      	
      	$this->data['sort_menuname'] = HTTPS_SERVER . 'index.php?route=menu/menu&token=' . $this->session->data['token'] . '&sort=menuname' . $url;
      	$this->data['sort_ordering'] = HTTPS_SERVER . 'index.php?route=menu/menu&token=' . $this->session->data['token'] . '&sort=ordering' . $url;
      	$this->data['sort_menu_id'] = HTTPS_SERVER . 'index.php?route=menu/menu&token=' . $this->session->data['token'] . '&sort=menu_id' . $url;
      	
      	$this->data['sort'] = $sort;
      	$this->data['order'] = $order;
      	
      	$this->template = 'menu/menu_list.tpl';
      	$this->children = array(
			'common/header',	
			'common/footer'	
		);

		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
	}
	
	private function getForm() {
		$this->data['heading_title'] = $this->language->get('heading_title');

    	$this->data['entry_menuname'] = $this->language->get('entry_menuname');
    	$this->data['entry_pid'] = $this->language->get('entry_pid');
    	$this->data['entry_ordering'] = $this->language->get('entry_ordering');
		
		//$this->data['entry_captcha'] = $this->language->get('entry_captcha');
		
    	$this->data['select_root'] = $this->language->get('select_root');
		
		$this->data['button_save'] = $this->language->get('button_save');
    	$this->data['button_cancel'] = $this->language->get('button_cancel');

    	$this->data['tab_general'] = $this->language->get('tab_general');
    	
    	//錯誤信息設置
		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		
		if (isset($this->error['menuname'])) {
			$this->data['error_menuname'] = $this->error['menuname'];
		} else {
			$this->data['error_menuname'] = '';
		}

 		if (isset($this->error['pid'])) {
			$this->data['error_pid'] = $this->error['pid'];
		} else {
			$this->data['error_pid'] = '';
		}
		
 		if (isset($this->error['ordering'])) {
			$this->data['error_ordering'] = $this->error['ordering'];
		} else {
			$this->data['error_ordering'] = '';
		}
		
		if (isset($this->error['pidmatch'])) {
			$this->data['error_pid_match'] = $this->error['pidmatch'];
		} else {
			$this->data['error_pid_match'] = '';
		}
		
		$url = '';
		
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
		
    	$this->document->breadcrumbs = array();

   		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=common/home&token=' . $this->session->data['token'],
       		'text'      => $this->language->get('text_home'),
      		'separator' => FALSE
   		);

   		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=menu/menu&token=' . $this->session->data['token'] . $url,
       		'text'      => $this->language->get('heading_title'),
      		'separator' => ' :: '
   		);
   		
		if (!isset($this->request->get['menu_id'])) {
			$this->data['action'] = HTTPS_SERVER . 'index.php?route=menu/menu/insert&token=' . $this->session->data['token'] . $url;
		} else {
			$this->data['action'] = HTTPS_SERVER . 'index.php?route=menu/menu/update&token=' . $this->session->data['token'] . '&menu_id=' . $this->request->get['menu_id'] . $url;
		}
		
		$this->data['cancel'] = HTTPS_SERVER . 'index.php?route=menu/menu&token=' . $this->session->data['token'] . $url;
		
		//menus list init
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'menuname';
		}

		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}
		
		$this->data['menus'] = array();

      	$data = array(
			'sort'  => $sort,
			'order' => $order
      	);
      	
      	$results = $this->model_menu_menu->getTopMenus($data);

      	foreach ($results as $result) {
      		$subMenuIds = $this->model_menu_menu->strToArray($this->model_menu_menu->getIds($result['menu_id'], $result['menu_id'].'/'), '/');
      		foreach ($subMenuIds as $subMenuId) {
      			if ($subMenuId != '') {
      				
      				$action = array();
      				
	      			$oneMenu = $this->model_menu_menu->getMenuById($subMenuId);
	      			$action[] = array(
						'text' => $this->language->get('text_edit'),
						'href' => HTTPS_SERVER . 'index.php?route=menu/menu/update&token=' . $this->session->data['token'] . '&menu_id=' . $oneMenu['menu_id'] . $url
	      			);
	
	      			$this->data['menus'][] = array(
						'menu_id'    => $oneMenu['menu_id'],
						'pid'   => $oneMenu['pid'],
	      				'menuname'   => $oneMenu['menuname'],
	      				'ordering'   => $oneMenu['ordering'],
	      				'sublevel'   => $oneMenu['sublevel'],
	      				'createdate'   => date($this->language->get('date_format_short'), strtotime($oneMenu['createdate'])),
	      				'updatedate'   => date($this->language->get('date_format_short'), strtotime($oneMenu['updatedate'])),
						'selected'   => isset($this->request->post['selected']) && in_array($oneMenu['user_id'], $this->request->post['selected']),
						'action'     => $action
	      			);
      			}
      		}
      	}
      	
		if (isset($this->request->get['menu_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
      		$menu_info = $this->model_menu_menu->getMenuById($this->request->get['menu_id']);
    	}
    	
		if (isset($this->request->post['menuname'])) {
      		$this->data['menuname'] = $this->request->post['menuname'];
    	} elseif (isset($menu_info)) {
			$this->data['menuname'] = $menu_info['menuname'];
		} else {
      		$this->data['menuname'] = '';
    	}
  
    	if (isset($this->request->post['pid'])) {
      		$this->data['pid'] = $this->request->post['pid'];
    	} elseif (isset($menu_info)) {
			$this->data['pid'] = $menu_info['pid'];
		} else {
      		$this->data['pid'] = '';
    	}

    	if (isset($this->request->post['ordering'])) {
      		$this->data['ordering'] = $this->request->post['ordering'];
    	} elseif (isset($menu_info)) {
			$this->data['ordering'] = $menu_info['ordering'];
		} else {
      		$this->data['ordering'] = '0';
   		}
    	
		if (isset($this->request->get['menu_id'])) {
      		$this->data['menu_id'] = $this->request->get['menu_id'];
    	} else {
      		$this->data['menu_id'] = '';
   		}
   		
		$this->load->model('menu/menu');
    	
		$this->template = 'menu/menu_form.tpl';
		$this->children = array(
			'common/header',	
			'common/footer'	
		);
		
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
	}
	
	private function validateForm() {
    	if (!$this->user->hasPermission('modify', 'menu/menu')) {
      		$this->error['warning'] = $this->language->get('error_permission');
    	}
    
    	if ((strlen(utf8_decode($this->request->post['menuname'])) <= 0)) {
      		$this->error['menuname'] = $this->language->get('error_menuname');
    	}

    	if ((strlen(utf8_decode($this->request->post['pid'])) <= 0)) {
      		$this->error['pid'] = $this->language->get('error_pid');
    	} else if (isset($this->request->get['menu_id']) && utf8_decode($this->request->post['pid']) == utf8_decode($this->request->get['menu_id'])) {
    		$this->error['pidmatch'] = $this->language->get('error_pid_match');
    	}

    	if ((strlen(utf8_decode($this->request->post['ordering'])) <= 0)) {
      		$this->error['ordering'] = $this->language->get('error_ordering');
    	}
	
    	if (!$this->error) {
      		return TRUE;
    	} else {
      		return FALSE;
    	}
  	}
  	
	private function validateDelete() { 
    	if (!$this->user->hasPermission('modify', 'menu/menu')) {
      		$this->error['warning'] = $this->language->get('error_permission');
    	} 
	  	  
		foreach ($this->request->post['selected'] as $menu_id) {
			if ($this->model_menu_menu->getContentCountById($menu_id) > 0 || $this->model_menu_menu->getMenupCountById($menu_id) > 0) {
				$this->error['warning'] = $this->language->get('error_content');
			}
		}
		 
		if (!$this->error) {
	  		return TRUE;
		} else { 
	  		return FALSE;
		}
  	}
}
?>